package factory;

import comgame.components.*;
import comgame.entities.EntityType;
import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.entity.*;
import com.almasb.fxgl.entity.components.CollidableComponent;
import com.almasb.fxgl.physics.BoundingShape;
import com.almasb.fxgl.physics.HitBox;
import com.almasb.fxgl.texture.Texture;
import comgame.entities.Player;

import static comgame.scenes.GameScene.player;

public class PlayerFactory implements EntityFactory {


    @Spawns("player") // 使用Spawns注解定义实体类型
    public Entity newPlayer(SpawnData data) {
        // 从生成数据获取位置
        double x = data.getX();
        double y = data.getY();
        // 创建基础纹理
        Texture originalTexture = FXGL.texture("player.png", 60, 60); // 指定加载为 96x96 像素
        // 先创建基础实体
        Entity playerEntity = FXGL.entityBuilder(data)
                .type(EntityType.PLAYER)
                .at(x, y)
                .view(originalTexture)
                .bbox(new HitBox(BoundingShape.box(originalTexture.getWidth(),
                        originalTexture.getHeight()))) // 修正缩放倍数
                .with(new SkillComponent())
                .with(new AttributeComponent())
                .with(new AnimationComponent())
                .with(new HealthComponent()) // 只添加一次
                .with(new CollidableComponent(true))
                .zIndex(10000)
                .build();
        // 用playerEntity创建Player实例
        Player player = new Player(playerEntity);
        int HP= (int) (100*Math.pow(1.1,player.getCombatLevel()-1));
        playerEntity.getComponent(HealthComponent.class).setMaxHealth(HP);
        // 设置玩家的基础属性值
        AttributeComponent attr = playerEntity.getComponent(AttributeComponent.class);
        if (attr != null) {
            attr.setAttack(5);   // 基础攻击力
            attr.setDefense(3);   // 基础防御力
            attr.setSpeed(2);     // 基础速度
            attr.setCritRate(0.05); // 基础暴击率
            attr.setBaseAttack(5);
            attr.setBaseDefense(3);
            attr.setBaseSpeed(2);
            attr.setBaseCritRate(0.05);
        }
        playerEntity.setProperty("player", player); // 将player对象附加到实体上
        return playerEntity;
    }
}